docs: Update Python cookbook examples#127
Conversation
- Convert all 5 recipe examples to async/await patterns matching current SDK interface * error_handling.py: Async/await conversion with proper event handling * managing_local_files.py: Async conversion with non-blocking user input * multiple_sessions.py: Full async/await conversion with proper cleanup * persisting_sessions.py: Async/await conversion with session management updates * pr_visualization.py: Async conversion with async subprocess handling Root cause: SDK interface changed from synchronous to asynchronous (async/await) API. All examples now use correct async patterns with proper error handling and cleanup."
There was a problem hiding this comment.
Pull request overview
This PR converts five Python cookbook recipe examples from synchronous to asynchronous patterns to align with the current SDK's async/await API. The SDK interface has migrated from synchronous methods to async methods, requiring all client operations, session management, and message sending to use async/await patterns.
Changes:
- Converted all SDK method calls to async/await (client.start(), create_session(), send_and_wait(), destroy(), stop())
- Updated event handling from dictionary access to dataclass attribute access (event.type, event.data)
- Added proper async cleanup patterns with try/finally blocks
- Implemented non-blocking user input using asyncio.get_event_loop().run_in_executor()
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 13 comments.
Show a summary per file
| File | Description |
|---|---|
| cookbook/python/recipe/pr_visualization.py | Converted PR analysis tool to async with event loop-based user input, async session management, and proper cleanup |
| cookbook/python/recipe/persisting_sessions.py | Converted session persistence example to async, documenting unavailable list_sessions/delete_session methods |
| cookbook/python/recipe/multiple_sessions.py | Converted multi-session example to async with sequential session creation and proper cleanup |
| cookbook/python/recipe/managing_local_files.py | Converted file management example to async with non-blocking user input loop |
| cookbook/python/recipe/error_handling.py | Converted error handling example to async with proper exception handling and cleanup |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…g and managing local files
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@aaronpowell do you mind taking a look? |
|
@friggeri good to merge |
|
Is there a reason for moving away from kwargs to dict? |
Yes, the move to a dictionary is due to changes in the underlying SDK’s create_session method signature. In the latest async implementation (see python/copilot/client.py), the method is defined as:async def create_session(self, config: Optional[SessionConfig] = None) It now expects a single config dictionary rather than individual keyword arguments (**kwargs). The official docstrings and E2E tests in the SDK also follow this pattern (passing a dict for model, tools, and other configurations). I updated the cookbook examples to align with this new interface. |
|
bad group, no body care. Never submit to any Microsoft product. GDD 🤮 Close it. |
|
Not sure why move to the dictionary which is very much non pythonic. |
Root cause: SDK interface changed from synchronous to asynchronous (async/await) API. All examples now use correct async patterns with proper error handling and cleanup."